#include "Runtime.h"

Runtime * Runtime::instance_ = 0;
Runtime * Runtime::instance()
{
  if (0 == instance_)
    instance_ = new Runtime;

  return instance_;
}

int Runtime::init(T_MODE t, R_MODE r, int thr_n)
{
  if (singlethread == t && selectreactor == r)
    {
      ACE_NEW_RETURN(reactor, ACE_Reactor, -1);
      ACE_Reactor::instance(reactor);
      ACE_NEW_RETURN(task, SingleTask(reactor), -1);
    }
  else if (threadpool == t && tpreactor == r)
    {
      ACE_TP_Reactor *tpreactor;
      ACE_NEW_RETURN(tpreactor, ACE_TP_Reactor, -1);
      ACE_NEW_RETURN(reactor, ACE_Reactor(tpreactor), -1);
      ACE_Reactor::instance(tpreactor);
      ACE_NEW_RETURN(task, TPTask(reactor, thr_n), -1);
    }
  else
    return -1;

  return task->start();

}

void Runtime::stop()
{
  reactor == 0;

  if (0 != task)
    task->stop();
  task = 0;

  delete instance_;
  instance_ = 0;
}
